package com.rm2pt.generator.springboot.LabManage.controller;
import com.rm2pt.generator.springboot.LabManage.service.*;
import com.rm2pt.generator.springboot.LabManage.Message.*;
import java.util.Date;
import com.rm2pt.generator.springboot.LabManage.utils.PreconditionException;
import com.rm2pt.generator.springboot.LabManage.utils.StandardOPs;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping("/CreatePlanService")
public class CreatePlanServiceController {
	
	//@Autowired HttpSession hs;
	
	@Autowired 
	private CreatePlanService createPlanService;
	
	@RequestMapping(value="/createPlan",method=RequestMethod.PUT)	
	public String createPlan(CreatePlanMessage createPlanMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createPlanService.createPlan(createPlanMsg.groupId,createPlanMsg.id,createPlanMsg.studentId,createPlanMsg.content,createPlanMsg.finish));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/editPlan",method=RequestMethod.PUT)	
	public String editPlan(EditPlanMessage editPlanMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createPlanService.editPlan(editPlanMsg.id,editPlanMsg.content));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	@RequestMapping(value="/finishPlan",method=RequestMethod.PUT)	
	public String finishPlan(FinishPlanMessage finishPlanMsg) {
		
		
		JSONObject object = new JSONObject();
		try {
			object.put("data", createPlanService.finishPlan(finishPlanMsg.id,finishPlanMsg.finish));
			object.put("msg", "success");
			object.put("code", "200");
		} catch (Exception e) {
			if (e instanceof PreconditionException) {
				object.put("msg", "PreConditionException");
			}else {
				object.put("msg", "PostConditionException");
				e.printStackTrace();
			}
			object.put("code", "400");
		}
		String s=JSON.toJSONString(object);
		return s;
	}
	
}
